package cn.itcast.controller;

import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderListController {

    @Reference
    private OrderService orderService;

    //分页查询
    @RequestMapping("findPage")
    Result findPage(@RequestParam Map<String, String> param) {
        PageResult pageResult = null;
        String result = null;
        try {
            //封装查询条件
            OrderQueryPageBean orderQueryPageBean = new OrderQueryPageBean();
            orderQueryPageBean.setMaxTime(param.get("maxTime"));
            orderQueryPageBean.setMinTime(param.get("minTime"));
            orderQueryPageBean.setOrderType(param.get("orderType"));
            if (param.get("orderStatus") != null) {
                orderQueryPageBean.setPayStatus(Integer.valueOf(param.get("orderStatus")));
            }
            orderQueryPageBean.setCurrentPage(Integer.valueOf(param.get("currentPage")));
            orderQueryPageBean.setPageSize(Integer.valueOf(param.get("pageSize")));

            if(StringUtils.isNotEmpty(param.get("queryString"))){
                orderQueryPageBean.setQueryString(param.get("queryString"));
            }

            if (param.get("payStatus") != null) {
                orderQueryPageBean.setPayStatus(Integer.valueOf(param.get("payStatus")));
            }
            //调用service层查询
            pageResult = orderService.findByPage(orderQueryPageBean);
        } catch (Exception e) {
            return new Result(false, "查询预约列表出错");
        }
        //返回结果集对象
        return new Result(true, "查询预约列表成功", pageResult);
    }


    //编辑页面回显
    @GetMapping("/{id}")
    Result findById(@PathVariable Long id) {
        Order order = null;
        try {
            order = orderService.findById(id);
        } catch (Exception e) {
            return new Result(false, "查询订单出错");
        }
        return new Result(true, "查询订单成功", order);
    }


    //编辑修改预约信息
    @PostMapping("submit")
    Result updateOrder(@RequestBody Order order) {
        try {
            orderService.updateOrder(order);
        } catch (Exception e) {
            return new Result(false, "编辑新增信息出错", e);
        }
        return new Result(true, "编辑新增信息成功");
    }

    //修改预约状态
    @PutMapping("/{id}/{arrival}")
    Result changeOrderStatus(@PathVariable("id") Long id, @PathVariable("arrival") Integer arrival) {
        try {
            orderService.changeOrderStatus(id, arrival);
        } catch (Exception e) {
            return new Result(false, "修改预约状态失败");
        }
        return new Result(true, "修改预约状态成功");
    }


}
